<template>
	<view class="page">
		<!-- #ifdef H5 -->
		<topVue title="兑换管理" :isBack="true" rightIcon="server-fill"></topVue>
		<!-- #endif -->

		<view class="main-content">
			<scroll-view class="content" scroll-y>
				<u-subsection :list="tabs" :current="current" button-color=" #3B82F6" active-color="#fff" bg-color="#fff" @change="change"></u-subsection>
				<template v-if="current===0">
					<view class="section margin30">
						<view class="label">生成兑换码<text>(还可生成{{totalNumber-(spNumber+wxNumber)}}个)</text></view>
						<view class="flex-between align-center margin20">
							<view class="ket">AI视频兑换码</view>
							<u-number-box v-model="spNumber" :max="totalNumber-wxNumber"></u-number-box>
						</view>
						<view class="flex-between align-center margin20">
							<view class="ket">AI微信兑换码</view>
							<u-number-box v-model="wxNumber" :max="totalNumber-spNumber"></u-number-box>
						</view>
						<view class="btn margin30" @click="generateRedemptionCode()">生成</view>
					</view>

					<view class="flex-between margin30">
						<view class="info flex-between flex-column align-center padding-column25">
							<text
								style="color: #000; font-weight: 600; font-size: 32rpx;">{{redemptionCode.videoTotal?redemptionCode.videoUsed+"/"+redemptionCode.videoTotal:"0/0"}}</text>
							<text style="color: #5F6671;">已使用AI视频兑换码</text>
						</view>
						<view class="info flex-between flex-column align-center padding-column25">
							<text
								style="color: #000; font-weight: 600; font-size: 32rpx;">{{redemptionCode.wechatTotal?redemptionCode.wechatUsed+"/"+redemptionCode.wechatTotal:"0/0"}}</text>
							<text style="color: #5F6671;">已使用AI微信兑换码</text>
						</view>
					</view>

					<view class="section margin30">
						<view class="searchVue flex align-center padding-row30">
							<u-icon name="search" size="36" color="#B3B7C0"></u-icon>
							<input type="text" placeholder="搜索兑换码" v-model="inputCode" @input="getSearchRedemptionCode()" />
						</view>
						<u-dropdown class="u-dropdown">
							<u-dropdown-item v-model="activeState" :title="activeState" :options="options1" @change="getRedemptionCodeList()"></u-dropdown-item>
							<u-dropdown-item v-model="redemptionType" :title="redemptionType" :options="options2" @change="getRedemptionCodeList()"></u-dropdown-item>
						</u-dropdown>
						<view class="w100 flex-column align-center padding-column20" v-if="isEmpty">
							<image src="../../static/empty.png" style="width: 256rpx; height: 256rpx;"></image>
							<text style="color: #81868e;">{{describe}}</text>
						</view>
						<view class="item margin40 flex-between align-center"
							v-for="(item, index) in redemptionCodeList" :key="index">
							<view class="flex-column">
								<view class="flex align-center">
									<view :class="[item.type==='AI_WECHAT'?'redemptionWeChatLabel':'redemptionVideoLabel']">{{item.type==="AI_WECHAT"?'微信':'视频'}}</view>
									<view class="name">{{item.code}}</view>
									<view class="state">{{item.used?'已使用':'未使用'}}</view>
								</view>
							</view>
							<view class="copy" @click="$tools.copy(item.code)">复制</view>
						</view>
					</view>
				</template>

				<template v-else>
					<view class="section margin30">
						<view class="label">生成兑换码</view>
						<view class="flex-between align-center margin20">
							<view class="ket">AI视频兑换码<text>(价格：398元/年)</text></view>
							<u-number-box v-model="purchaseSpNumber"></u-number-box>
						</view>
						<view class="flex-between align-center margin20">
							<view class="ket">AI微信兑换码<text>(价格：398元/年)</text></view>
							<u-number-box v-model="purchaseWxNumber"></u-number-box>
						</view>
						<view class="flex-between align-center margin30">
							<view class="flex-1" style="font-weight: 600; font-size: 34rpx; color: #000;">￥{{(purchaseSpNumber+purchaseWxNumber)*398}}</view>
							<view class="btn" style="width: 160rpx; border-radius: 40rpx;" @click="buyFun()">支付</view>
						</view>
					</view>
				</template>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				wxNumber: 0,
				spNumber: 0,
				purchaseWxNumber: 1,
				purchaseSpNumber: 1,
				totalNumber: 0,
				tabs: [{ name: "生成兑换码" }, { name: "采购兑换码" }],
				current: 0,
				inputCode: '',
				isEmpty: false,
				redemptionCode: {},
				redemptionCodeList: [],
				redemptionType: 'AI视频',
				activeState: '未使用',
				describe: '暂无',
				options1: [{ label: '未使用', value: '未使用' }, { label: '已使用', value: '已使用' }],
				options2: [{ label: 'AI视频', value: 'AI视频' }, { label: 'AI微信', value: 'AI微信' }],
			}
		},
		computed: {
			...mapState({
				user: state => state.user,
			})
		},
		onLoad() {
			setTimeout(() => {
				this.initialization()
			}, 500)
		},
		methods: {
			change(e) {
				this.current = e
			},

			async initialization() {
				this.getRedemptionCode()
				this.getRedemptionCodeList()
			},
			/**
			 * 获取兑换码统计
			 * @returns {Promise<void>}
			 */
			async getRedemptionCode() {
				const res = await this.$request("/aiet-user-center/user/redemptionCode/summary", {
					creatorId: this.user.id,
				}, "GET")
				if (res.code === 200) {
					this.redemptionCode = res.data;
					let total = res.data.redemptionCodeNum - ( res.data.wechatTotal +  res.data.videoTotal)
					this.totalNumber =  total> 0 ? total : 0
					// this.spNumber = this.totalNumber >= 1 ? 1 : 0
					// this.wxNumber = this.totalNumber -1 < 1 ? 0 : 1
				}
				console.log("获取兑换码统计", this.redemptionCode)
			},
			/**
			 * 生成兑换码
			 * @returns {Promise<void>}
			 */
			async generateRedemptionCode() {
				if (this.spNumber < 1 && this.wxNumber < 1) return this.$u.toast("请选择生成数量")
				const res = await this.$request("/aiet-user-center/user/redemptionCode", {
					aiVideo: this.spNumber,
					aiWechat: this.wxNumber
				}, "POST")
				if (res.code === 200) {
					this.$u.toast("生成兑换码成功")
					this.initialization()
				} else {
					this.$u.toast(res.msg)
				}
				console.log("获取兑换码统计", this.redemptionCode)
			},
			/**
			 * 搜索兑换码
			 * @returns {Promise<void>}
			 */
			async getSearchRedemptionCode() {
				if (this.inputCode === '' || this.inputCode.length < 1) return this.getRedemptionCodeList()
				const res = await this.$request("/aiet-user-center/user/redemptionCode/search", {
					code: this.inputCode,
				}, "GET")
				if (res.code === 200) {
					this.redemptionCodeList = res.data
					this.isEmpty = res.data.length === 0
					this.describe = '查询兑换码为空'
				}
				console.log("搜索兑换码", this.redemptionCodeList)
			},
			/**
			 * 获取兑换码列表
			 * @returns {Promise<void>}
			 */
			async getRedemptionCodeList() {
				const res = await this.$request("/aiet-user-center/user/redemptionCode/list", {
					creatorId: this.user.id,
					used: this.activeState === '未使用' ? false : true,
					type: this.redemptionType === 'AI微信' ? 'AI_WECHAT' : 'AI_VIDEO'
				}, "GET")
				if (res.code === 200) {
					this.redemptionCodeList = res.data
					this.isEmpty = res.data.length === 0
					this.describe = '暂无' + this.redemptionType + this.activeState + '兑换码'
				}
				console.log("获取兑换码列表", this.redemptionCodeList)
			},
			/**
			 * 立即付款
			 * @returns {Promise<*>}
			 */
			async buyFun() {
				if(this.purchaseSpNumber===0&&this.purchaseWxNumber===0) return this.$u.toast("请选择购买数量")
				const res = await this.$request("/aiet-user-center/user/orders/pay", {
					productName: "购买"+this.purchaseSpNumber+"个视频兑换码和"+this.purchaseWxNumber+"个微信兑换码",
					orderAmount: (this.purchaseSpNumber + this.purchaseWxNumber) * 398,
					platform: "数字人-H5",
					productType: 3,
					videoRedemptionCodeNum: this.purchaseSpNumber,
					wechatRedemptionCodeNum: this.purchaseWxNumber,
					manufacturer: 1
				}, "POST", "application/json")
				if (res.code === 200) {
					console.log(res.data.payUrl)
					// #ifdef H5
					 window.location.href = res.data.payUrl
					// #endif
				}
			},

		}
	}
</script>

<style lang="less" scoped>
	.main-content {
		width: 100%;
		height: calc(100vh - var(--window-bottom) - var(--window-top) - 92rpx);
		padding: 30rpx;
		box-sizing: border-box;
		background: linear-gradient(to bottom, #10B981, #3B82F6);

		.content {
			width: 100%;
			height: calc(100vh - var(--window-bottom) - var(--window-top) - 122rpx);
			border-radius: 12rpx;
			box-sizing: border-box;

			.section {
				width: 100%;
				padding: 30rpx;
				background: #fff;
				border-radius: 12rpx;

				.label {
					font-weight: 600;
					font-size: 32rpx;

					text {
						color: #9EA7A8;
					}
				}

				.ket {
					color: #333;

					text {
						color: #9EA7A8;
					}
				}

				input {
					font-size: 28rpx;
					padding-left: 20rpx;
				}

				.btn {
					width: 100%;
					height: 80rpx;
					background: linear-gradient(to right, #76D98A, #4E82EB);
					line-height: 80rpx;
					text-align: center;
					color: #fff;
					font-size: 30rpx;
				}

				.searchVue {
					width: 100%;
					height: 80rpx;
					background: #F3F4F6;
					border-radius: 40rpx;
				}

				.item {
					width: 100%;

					.copy {
						width: 110rpx;
						height: 60rpx;
						background: #4E80EE;
						border-radius: 10rpx;
						display: flex;
						font-weight: 600;
						justify-content: center;
						align-items: center;
						font-size: 24rpx;
						line-height: 72rpx;
						text-align: center;
						color: #fff;
					}

					.name {
						font-weight: 600;
						color: #000;
						font-size: 30rpx;
						line-height: 1;
					}

					.redemptionWeChatLabel {
						padding: 6rpx 10rpx;
						font-size: 20rpx;
						background-color: #48D685;
						border-radius: 6rpx;
						color: #FFFFFF;
						margin-right: 12rpx;
					}

					.redemptionVideoLabel {
						padding: 6rpx 10rpx;
						font-size: 20rpx;
						background-color: #3C87EF;
						border-radius: 6rpx;
						color: #FFFFFF;
						margin-right: 12rpx;
					}

					.state {
						padding-left: 20rpx;
						color: #E47A77;
						font-weight: 600;
					}

					.phone {
						line-height: 1;
						color: #333;
						padding-top: 15rpx;
					}
				}
			}

			.info {
				width: 330rpx;
				height: 140rpx;
				border-radius: 12rpx;
				background: #fff;
			}
		}
	}
</style>
